<template>
  <div class="Index">
    <CommonFormCreate ref="form" v-model="form" :options="options" />
    <div class="btn" hover-class="hover" hover-start-time="0" hover-stay-time="0" @click="handleAdd">提交建议</div>
  </div>
</template>

<script>
import CommonFormCreate from '@/components/CommonFormCreate'
import TYPE from '@/components/CommonFormCreateType'
import api from '@/api'
import { formatDate } from '@/utils'

export default {
  components: {
    CommonFormCreate,
  },
  data () {
    return {
      form: {
        title: '',
        type: '',
        primaryUserInfoId: this.$store.state.userInfo.userInfoId,
        primaryUserInfoName: this.$store.state.userInfo.realName,
        userInfoIds: [],
        content: '',
        files: [],
        adviceTime: formatDate(new Date(), 'yyyy-MM-dd HH:mm'),
      },
      options: [
        {
          type: TYPE.SELECT,
          title: '建议分类',
          field: 'type',
          dictType: 'advice_type',
          required: true,
          placeholder: '请选择建议分类',
          span: 2,
        },
        {
          type: TYPE.INPUT,
          title: '建议标题',
          field: 'title',
          required: true,
          placeholder: '请输入建议标题',
          span: 2,
          width: '100%',
        },
        {
          type: TYPE.TEXTAREA,
          title: '建议内容',
          field: 'content',
          required: true,
          placeholder: '请输入建议内容',
          span: 2,
        },
        {
          type: TYPE.FILE_UPLOAD,
          title: '附件',
          field: 'files',
        },
        {
          type: TYPE.INPUT,
          title: '领衔代表',
          field: 'primaryUserInfoName',
          disabled: true,
        },
        {
          type: TYPE.SEARCH_SELECT,
          title: '联名代表',
          field: 'userInfoIds',
          placeholder: '输入名字进行搜索',
          searchType: 'user',
          zIndex: 99,
        },
        {
          type: TYPE.DATE_SELECT,
          title: '提出时间',
          field: 'adviceTime',
          required: true,
          placeholder: '请选择提出时间',
          span: 2,
        },
      ],
    }
  },
  onLoad () {
    if (!this.$store.state.userInfo.npcmember) {
      uni.showModal({
        title: '提示',
        content: '您不是人大代表, 不能提交建议',
        showCancel: false,
        success (res) {
          if (res.confirm) {
            uni.navigateBack()
          }
        },
      })
    }
  },
  methods: {
    async handleAdd () {
      await this.$refs.form.$validate()
      await api.addAdvice(this.dealForm(this.form))
      uni.showModal({
        title: '成功',
        content: '添加成功',
        showCancel: false,
      })
      this.$store.commit('SET_NEED_ADVICE_REFRESH_TIME', Date.now())
      uni.navigateBack()
    },
    dealForm (form) {
      const result = { ...form }
      result.adviceTime && (result.adviceTime = new Date(result.adviceTime).getTime())
      result.files = result.files.map(item => item.id)
      delete result.primaryUserInfoName
      return result
    },
  },
}
</script>

<style lang="scss" scoped> @import "~css/var.scss";

.Index {
	overflow: hidden;
	min-height: 100vh;
	padding: 0 15px;
	background: #fff;

	.btn {
		margin: 25px 0;
		border-radius: 5px;
		line-height: 46px;
		text-align: center;
		font-weight: 500;
		font-size: $fz-xl;
		color: #fff;
		background: #d1272f;

		&.hover {
			opacity: .8;
		}
	}
}

</style>
